{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #有效的字母异位词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isAnagram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #有效的字母异位词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个字符串 <code>s</code> 和 <code>t</code> ，编写一个函数来判断它们是不是一组变位词（字母异位词）。</p>\n",
    "\n",
    "<p><strong>注意：</strong>若&nbsp;<code><em>s</em></code> 和 <code><em>t</em></code><em>&nbsp;</em>中每个字符出现的次数都相同且<strong>字符顺序不完全相同</strong>，则称&nbsp;<code><em>s</em></code> 和 <code><em>t</em></code><em>&nbsp;</em>互为变位词（字母异位词）。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例&nbsp;1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = &quot;anagram&quot;, t = &quot;nagaram&quot;\n",
    "<strong>输出:</strong> true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = &quot;rat&quot;, t = &quot;car&quot;\n",
    "<strong>输出: </strong>false</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = &quot;a&quot;, t = &quot;a&quot;\n",
    "<strong>输出: </strong>false</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length, t.length &lt;= 5 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>s</code>&nbsp;and&nbsp;<code>t</code>&nbsp;仅包含小写字母</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>进阶:&nbsp;</strong>如果输入字符串包含 unicode 字符怎么办？你能否调整你的解法来应对这种情况？</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><meta charset=\"UTF-8\" />注意：本题与主站 242&nbsp;题相似（字母异位词定义不同）：<a href=\"https://leetcode-cn.com/problems/valid-anagram/\">https://leetcode-cn.com/problems/valid-anagram/</a></p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [dKk3P7](https://leetcode.cn/problems/dKk3P7/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [dKk3P7](https://leetcode.cn/problems/dKk3P7/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s)!=len(t) or s==t:\n",
    "            return False\n",
    "        need = collections.defaultdict(int)\n",
    "        for c in t:\n",
    "            need[c] += 1\n",
    "        count = len(t)\n",
    "        for c in s:\n",
    "            if need[c]==0:\n",
    "                return False\n",
    "            need[c] -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return sorted(s) == sorted(t) and s != t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return s != t and sorted(s) == sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return sorted(s) == sorted(t) and s!=t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "\n",
    "        s_list = sorted(s)\n",
    "        t_list = sorted(t)\n",
    "\n",
    "        if s_list == t_list:\n",
    "            return True\n",
    "\n",
    "        return False\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    obj = Solution()\n",
    "\n",
    "    s = \"anagram\"\n",
    "    t = \"nagaram\"\n",
    "\n",
    "    res = obj.isAnagram(s, t)\n",
    "    print(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t or len(s) != len(t):\n",
    "            return False\n",
    "        if sorted(s) == sorted(t):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return Counter(s) == Counter(t) and s != t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        # 两字符串的字符种类、数量相同，但字符串本身不相同\n",
    "        return sorted(s) == sorted(t) and s != t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        # 定义一个26个字幕,完全一模一样\n",
    "        record = [0]*26\n",
    "        flag = True #假设一摸一样\n",
    "        len_t = len(t)\n",
    "        len_s = len(s)\n",
    "        if len_s != len_t: #长度不相等\n",
    "            return False\n",
    "        for i in range(len_s):\n",
    "            if s[i] != t[i]:\n",
    "                    flag = False\n",
    "                    break\n",
    "        if flag:\n",
    "            return False #一摸一样\n",
    "\n",
    "        for sub_s in s:\n",
    "            record[ord(sub_s)-ord('a')]+=1\n",
    "\n",
    "        for sub_t in t:\n",
    "            record[ord(sub_t)-ord('a')]-=1\n",
    "        # print(map)\n",
    "        # 所有元素都为0，不能加起来总和\n",
    "        for i in range(26):\n",
    "            if record[i]!=0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        count = [0] * 26\n",
    "        if s == t:\n",
    "            return False\n",
    "        for char in s:\n",
    "            count[ord(char) - ord('a')] += 1\n",
    "        for char in t:\n",
    "            count[ord(char) - ord('a')] -= 1\n",
    "        for c in count:\n",
    "            if c != 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s_dic=Counter(s)\n",
    "        t_dic=Counter(t)\n",
    "        return s_dic==t_dic and s!=t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        dic = collections.defaultdict(int)\n",
    "        for c in s:\n",
    "            dic[c] += 1\n",
    "        for c in t:\n",
    "            dic[c] -= 1\n",
    "            if dic[c] < 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        from collections import Counter\n",
    "        cs, ct = Counter(s), Counter(t)\n",
    "        if cs == ct and s != t:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s ==t or len(s)!=len(t):\n",
    "            return False\n",
    "        tabel = [0]*26\n",
    "        for i in range(len(s)):\n",
    "            tabel[ord(s[i]) - ord('a')]+=1\n",
    "        for i in range(len(t)):\n",
    "            tabel[ord(t[i])-ord('a')]-=1\n",
    "            if tabel[ord(t[i])-ord('a')]<0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t:\n",
    "            return False\n",
    "        elif Counter(s)==Counter(t):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t: return False\n",
    "        maps, mapt = dict(), dict()\n",
    "        for x in s:\n",
    "            if x in maps:\n",
    "                maps[x] += 1\n",
    "            else:\n",
    "                maps[x] = 1\n",
    "        for x in t:\n",
    "            if x in mapt:\n",
    "                mapt[x] += 1\n",
    "            else:\n",
    "                mapt[x] = 1\n",
    "        if maps == mapt: return True\n",
    "        else: return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic1,dic2=[0]*26,[0]*26\n",
    "        for i in s:\n",
    "            dic1[ord(i)-ord('a')]+=1\n",
    "        for j in t:\n",
    "            dic2[ord(j)-ord('a')]+=1\n",
    "        return dic1==dic2 and s!=t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        return Counter(s) == Counter(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return not(s==t) and Counter(s)==Counter(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        # 基础算法hash表\n",
    "        res = [0 for i in range(26)]\n",
    "        for i in s:\n",
    "            res[ord(i)-ord(\"a\")]+=1\n",
    "        for j in t:\n",
    "            res[ord(j)-ord(\"a\")]-=1\n",
    "        return all(map(lambda i:i==0,res)) and s!=t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic1,dic2={},{}\n",
    "        for i in s:\n",
    "            dic1[i]=dic1.get(i,0)+1\n",
    "        for i in t:\n",
    "            dic2[i]=dic2.get(i,0)+1\n",
    "        return dic1==dic2 and s!=t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return Counter(s) == Counter(t) and s != t\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        my_list =[0]*26\n",
    "        m,n = len(s),len(t)\n",
    "        if m != n or s == t:\n",
    "            return False\n",
    "        for i in s:\n",
    "            my_list[ord(i)-ord('a')]+=1\n",
    "        for j in t:\n",
    "            my_list[ord(j) -ord('a')]-=1 \n",
    "        for x in my_list:\n",
    "            if x != 0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        cnt = Counter()\n",
    "        for ch in s:\n",
    "            cnt[ch] += 1\n",
    "        for ch in t:\n",
    "            cnt[ch] -= 1\n",
    "        return all(v == 0 for v in cnt.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if t == s or len(t) != len(s):\n",
    "            return False\n",
    "        record = [0] * 26\n",
    "        for i in s:\n",
    "            #并不需要记住字符a的ASCII，只要求出一个相对数值就可以了\n",
    "            record[ord(i) - ord(\"a\")] += 1\n",
    "        for i in t:\n",
    "            record[ord(i) - ord(\"a\")] -= 1\n",
    "        for i in range(26):\n",
    "            if record[i] != 0:\n",
    "                #record数组如果有的元素不为零0，说明字符串s和t 一定是谁多了字符或者谁少了字符。\n",
    "                return False\n",
    "      \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic1 = collections.defaultdict(int)\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        for i in range(len(s)):\n",
    "            dic1[s[i]] +=1\n",
    "        for i in range(len(t)):\n",
    "            if t[i] not in dic1:\n",
    "                return False\n",
    "            dic1[t[i]] -= 1\n",
    "            if dic1[t[i]] < 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t:\n",
    "            return False\n",
    "        statistic_dict = {}\n",
    "        for s_ in s:\n",
    "            if s_ not in statistic_dict:\n",
    "                statistic_dict[s_] = 0\n",
    "            statistic_dict[s_] += 1\n",
    "        \n",
    "        for t_ in t:\n",
    "            if t_ not in statistic_dict:\n",
    "                return False\n",
    "            else:\n",
    "                statistic_dict[t_] -= 1\n",
    "                if statistic_dict[t_] == 0:\n",
    "                    statistic_dict.pop(t_)\n",
    "        \n",
    "        if len(statistic_dict)==0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t or len(s)!=len(t): return False\n",
    "        for c in set(s):\n",
    "            if s.count(c)!=t.count(c):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        return s!=t and sorted(s)==sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic1,dic2={},{}\n",
    "        for i in s:\n",
    "            dic1[i]=dic1.get(i,0)+1\n",
    "        for j in t:\n",
    "            dic2[j]=dic2.get(j,0)+1\n",
    "        return dic1==dic2 and s!=t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "\n",
    "        hashmap1 = defaultdict(int)\n",
    "        for item_s in s:\n",
    "            hashmap1[item_s] += 1\n",
    "\n",
    "        for item_t in t:\n",
    "            hashmap1[item_t] -= 1\n",
    "\n",
    "        for i in hashmap1.values():\n",
    "            if i != 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t or len(s) != len(t):\n",
    "            return False\n",
    "        _map = dict()\n",
    "        for i,c in enumerate(s):\n",
    "            if c not in _map:\n",
    "                _map[c] = 1\n",
    "            else:\n",
    "                _map[c] += 1\n",
    "        for c in t:\n",
    "            if c not in _map or _map[c] == 0:\n",
    "                return False\n",
    "            _map[c] -= 1\n",
    "        return True\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return Counter(s) == Counter(t) and s != t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:    \n",
    "        if s==t: return False\n",
    "        else:\n",
    "            dic_s={}\n",
    "            for c in s:\n",
    "                if c not in dic_s:\n",
    "                    dic_s[c]=1\n",
    "                else: dic_s[c]+=1\n",
    "\n",
    "            dic_t={}\n",
    "            for c in t:\n",
    "                if c not in dic_t:\n",
    "                    dic_t[c]=1\n",
    "                else: dic_t[c]+=1\n",
    "            \n",
    "            if dic_s==dic_t: return True\n",
    "            else: return False\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t or len(s) != len(t):\n",
    "            return False\n",
    "        l1 = list(sorted(list(s)))\n",
    "        l2 = list(sorted(list(t)))\n",
    "        if l1 == l2:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        ls=list(s)\n",
    "        lt=list(t)\n",
    "        if s==t:\n",
    "            return False\n",
    "        dct_ls=collections.Counter(ls)\n",
    "        dct_lt=collections.Counter(lt)\n",
    "\n",
    "        if dct_ls==dct_lt:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:     \n",
    "        return s != t and sorted(list(s))==sorted(list(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s_dic = {}\n",
    "        t_dic = {}\n",
    "        for c in s:\n",
    "            if c in s_dic:\n",
    "                s_dic[c] = s_dic[c]+1\n",
    "            else:\n",
    "                s_dic[c] = 1\n",
    "        for c in t:\n",
    "            if c in t_dic:\n",
    "                t_dic[c] = t_dic[c]+1\n",
    "            else:\n",
    "                t_dic[c] = 1\n",
    "        print(s_dic)\n",
    "        print(t_dic)\n",
    "        if s_dic==t_dic and s!=t:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s_l = sorted(list(s))\n",
    "        t_l = sorted(list(t))\n",
    "        if s_l != t_l:\n",
    "            return False\n",
    "        if s == t:\n",
    "            return False\n",
    "        return True\n",
    "            \n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s)!=len(t) or s == t:\n",
    "            return False\n",
    "        slist = list(s)\n",
    "        tlist = list(t)\n",
    "        slist.sort()\n",
    "        tlist.sort()\n",
    "        print(slist)\n",
    "        print(tlist)\n",
    "        if slist == tlist:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        x = set(list(s))\n",
    "\n",
    "        for i in x:\n",
    "            if s.count(i) != t.count(i):\n",
    "                return False\n",
    "        for j in range(len(s)):\n",
    "            if s[j] != t[j]:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return s != t and sorted(list(s)) == sorted(list(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return s != t and sorted(list(s)) == sorted(list(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        else:\n",
    "            string_list=list(set(list(s)+list(t)))\n",
    "            right=True\n",
    "            for x in string_list:\n",
    "                if s.count(x) == t.count(x):\n",
    "                    pass\n",
    "                else:\n",
    "                    right=False\n",
    "                    break\n",
    "            if right:\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t or len(s)!=len(t):\n",
    "            return False\n",
    "        #因此字符串排序的主要方法是将字符串转换成字符数组， 然后借用sorted函数进行排序， 最后用join方法重新拼装字符串。\n",
    "        s=''.join(sorted(s))\n",
    "        t=''.join(sorted(t))\n",
    "        return s==t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        if s == t:\n",
    "            return False\n",
    "        s1 = [ord(x) for x in s]\n",
    "        s2 = [ord(x) for x in t]\n",
    "        s1 = sorted(s1)\n",
    "        s2 = sorted(s2)\n",
    "\n",
    "        return s1 == s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        s = ''.join(sorted(s))\n",
    "        t = ''.join(sorted(t))\n",
    "        return s==t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t or len(s) != len(t):\n",
    "            return False\n",
    "\n",
    "        arr1 = list(s)\n",
    "        arr2 = list(t)\n",
    "        count_arr = [0 for i in range(26)]\n",
    "        for i in range(len(arr1)):\n",
    "            count_arr[ord(arr1[i]) - ord('a')] += 1\n",
    "            count_arr[ord(arr2[i]) - ord('a')] -= 1\n",
    "\n",
    "        for num in count_arr:\n",
    "            if num != 0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t or len(s)!=len(t):\n",
    "            return False\n",
    "        s=''.join(sorted(s))\n",
    "        t=''.join(sorted(t))\n",
    "        return s==t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        \n",
    "        m = len(s)\n",
    "        n = len(t)\n",
    "        if s == t or m != n:\n",
    "            return False\n",
    "        \n",
    "        sorted_s = ''.join(sorted(s))\n",
    "        sorted_t = ''.join(sorted(t))\n",
    "\n",
    "        return sorted_s == sorted_t\n",
    "\n",
    "        # hash_map = {}\n",
    "\n",
    "        # for i in range(m):\n",
    "        #     hash_map[s[i]] = hash_map.get(s[i], 0) + 1\n",
    "        # for i in range(n):\n",
    "        #     if t[i] in hash_map:\n",
    "        #         hash_map[t[i]] -= 1\n",
    "        #     else:\n",
    "        #         return False\n",
    "        #     if hash_map[t[i]] < 0:\n",
    "        #         return False\n",
    "\n",
    "        # return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        ss, ts = list(s), list(t)\n",
    "        ss.sort()\n",
    "        ts.sort()\n",
    "        return ss == ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s_l = list(s)\n",
    "        s_l2 = sorted(s_l)\n",
    "        t_l = list(t)\n",
    "        t_l2 = sorted(t_l)\n",
    "\n",
    "        if s_l2 == t_l2 and s_l !=  t_l:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if(s==t): \n",
    "            return False;\n",
    "        l1=list(s)\n",
    "        l2=list(t)\n",
    "        return sorted(l1)==sorted(l2);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        return sorted(s) == sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        return sorted(s) == sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t:return False\n",
    "        if sorted(s)==sorted(t):return True\n",
    "        else:return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s==t:\n",
    "            return False\n",
    "        if sorted(s)==sorted(t):\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t or len(s) != len(t):\n",
    "            return False\n",
    "\n",
    "        arr1 = list(s)\n",
    "        arr2 = list(t)\n",
    "        count_arr = [0 for i in range(26)]\n",
    "        for i in range(len(arr1)):\n",
    "            count_arr[ord(arr1[i]) - ord('a')] += 1\n",
    "            count_arr[ord(arr2[i]) - ord('a')] -= 1\n",
    "\n",
    "        for num in count_arr:\n",
    "            if num != 0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return s != t and sorted(s) == sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return sorted(s)==sorted(t) and s!=t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) == 1 or len(t) == 1:\n",
    "            return False\n",
    "        if s == t:\n",
    "            return False\n",
    "        strted_s = sorted(s)\n",
    "        strted_t = sorted(t)\n",
    "        if strted_s == strted_t:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return sorted(s) == sorted(t) and s != t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:    \n",
    "        ## 计数法\n",
    "        # if s==t: return False\n",
    "        # else:\n",
    "        #     dic_s={}\n",
    "        #     for c in s:\n",
    "        #         if c not in dic_s:\n",
    "        #             dic_s[c]=1\n",
    "        #         else: dic_s[c]+=1\n",
    "\n",
    "        #     dic_t={}\n",
    "        #     for c in t:\n",
    "        #         if c not in dic_t:\n",
    "        #             dic_t[c]=1\n",
    "        #         else: dic_t[c]+=1\n",
    "            \n",
    "        #     if dic_s==dic_t: return True\n",
    "        #     else: return False\n",
    "        \n",
    "        ## 排序\n",
    "        if s==t: return False\n",
    "        else:\n",
    "            sorted_s=sorted(s)\n",
    "            sorted_t=sorted(t)\n",
    "            if sorted_s==sorted_t:\n",
    "                return True\n",
    "            else: return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        else:\n",
    "            t1 = {_ for _ in s}\n",
    "            t2 = {_ for _ in t}\n",
    "            if t1 == t2:\n",
    "                s=[_ for _ in s]\n",
    "                t=[_ for _ in t]\n",
    "                s.sort()\n",
    "                t.sort()\n",
    "                if s==t:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        else:\n",
    "            s=[_ for _ in s]\n",
    "            t=[_ for _ in t]\n",
    "            s.sort()\n",
    "            t.sort()\n",
    "            if s==t:\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        sorted_s = sorted(s)\n",
    "        sorted_t = sorted(t)\n",
    "\n",
    "        return sorted_s == sorted_t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if s == t:\n",
    "            return False\n",
    "        sorted_s = sorted(s)\n",
    "        sorted_t = sorted(t)\n",
    "\n",
    "        return sorted_s == sorted_t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return s != t and sorted(s) == sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        else:\n",
    "            s=[_ for _ in s]\n",
    "            t=[_ for _ in t]\n",
    "            s.sort()\n",
    "            t.sort()\n",
    "            if s==t:\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s_sorted = sorted(s)\n",
    "        t_sorted = sorted(t)\n",
    "\n",
    "        return s != t and s_sorted == t_sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return sorted(s) == sorted(t) and s != t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return s != t and ''.join(sorted(list(s))) == ''.join(sorted(list(t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        else:\n",
    "            s=[_ for _ in s]\n",
    "            t=[_ for _ in t]\n",
    "            s.sort()\n",
    "            t.sort()\n",
    "            if s==t:\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t) or s == t:\n",
    "            return False\n",
    "        else:\n",
    "            t1 = {_ for _ in s}\n",
    "            t2 = {_ for _ in t}\n",
    "            if t1 == t2:\n",
    "                s=[_ for _ in s]\n",
    "                t=[_ for _ in t]\n",
    "                s.sort()\n",
    "                t.sort()\n",
    "                if s==t:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s=[i for i in s]\n",
    "        t=[i for i in t]\n",
    "        if s!=t and Counter(s)==Counter(t):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
